package com.example.calendar.data.repository

import com.example.calendar.data.local.dao.TaskDao
import com.example.calendar.data.model.Task
import com.example.calendar.data.model.TaskPriority
import kotlinx.coroutines.flow.Flow
import org.threeten.bp.LocalDateTime
import javax.inject.Inject
import javax.inject.Singleton

/**
 * Interface for task repository operations
 */
interface TaskRepository {
    suspend fun addTask(task: Task): String
    suspend fun updateTask(task: Task)
    suspend fun deleteTask(taskId: String)
    suspend fun completeTask(taskId: String, completed: Boolean)
    fun getTaskById(taskId: String): Flow<Task?>
    fun getAllTasks(): Flow<List<Task>>
    fun getCompletedTasks(): Flow<List<Task>>
    fun getIncompleteTasks(): Flow<List<Task>>
    fun getTasksForDate(date: LocalDateTime): Flow<List<Task>>
    fun getOverdueTasks(): Flow<List<Task>>
    fun getTasksByCategory(categoryId: String): Flow<List<Task>>
    fun getTasksByPriority(priority: TaskPriority): Flow<List<Task>>
    fun searchTasks(query: String): Flow<List<Task>>
    fun getIncompleteTaskCount(): Flow<Int>
}

/**
 * Implementation of TaskRepository that works with the Room database
 */
@Singleton
class TaskRepositoryImpl @Inject constructor(
    private val taskDao: TaskDao
) : TaskRepository {
    
    override suspend fun addTask(task: Task): String {
        taskDao.insertTask(task)
        return task.id
    }
    
    override suspend fun updateTask(task: Task) {
        taskDao.updateTask(task)
    }
    
    override suspend fun deleteTask(taskId: String) {
        taskDao.softDeleteTask(taskId)
    }
    
    override suspend fun completeTask(taskId: String, completed: Boolean) {
        taskDao.updateTaskCompletionStatus(taskId, completed)
    }
    
    override fun getTaskById(taskId: String): Flow<Task?> {
        return taskDao.getTaskById(taskId)
    }
    
    override fun getAllTasks(): Flow<List<Task>> {
        return taskDao.getAllTasks()
    }
    
    override fun getCompletedTasks(): Flow<List<Task>> {
        return taskDao.getTasksByCompletionStatus(true)
    }
    
    override fun getIncompleteTasks(): Flow<List<Task>> {
        return taskDao.getTasksByCompletionStatus(false)
    }
    
    override fun getTasksForDate(date: LocalDateTime): Flow<List<Task>> {
        return taskDao.getTasksForDate(date)
    }
    
    override fun getOverdueTasks(): Flow<List<Task>> {
        return taskDao.getOverdueTasks()
    }
    
    override fun getTasksByCategory(categoryId: String): Flow<List<Task>> {
        return taskDao.getTasksByCategory(categoryId)
    }
    
    override fun getTasksByPriority(priority: TaskPriority): Flow<List<Task>> {
        return taskDao.getTasksByPriority(priority)
    }
    
    override fun searchTasks(query: String): Flow<List<Task>> {
        return taskDao.searchTasks(query)
    }
    
    override fun getIncompleteTaskCount(): Flow<Int> {
        return taskDao.getIncompleteTaskCount()
    }
} 